package cn.fay.chapter15;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import cn.fay.chapter15.coffee.Coffee;
import cn.fay.chapter15.coffee.Latte;

public class Fill {
	public static <T> void fill(Addable<T> addable,Class<? extends T> clazz,int size) {
		for(int i=0;i<size;i++){
			try {
				addable.add(clazz.newInstance());
			} catch (Exception e) {
				throw new RuntimeException(e.getMessage());
			}
		}
	}
	public static <T> void fill(Addable<T> addable,Generator<T> gen,int size) {
		for(int i=0;i<size;i++){
			addable.add(gen.next());
		}
	}
	public static void main(String[] args) {
		List<Coffee> carrier=new ArrayList<Coffee>();
		Fill.fill(new AddableCollectionAdapter<Coffee>(carrier), Coffee.class, 3);
		Fill.fill(Adapter.collectionAdapter(carrier), Latte.class, 2);
		for (Coffee coffee : carrier) {
			System.out.println(coffee);
		}
	}
}
class AddableCollectionAdapter<T> implements Addable<T>{
	private Collection<T> collection;
	public AddableCollectionAdapter(Collection<T> collection) {
		this.collection=collection;
	}
	@Override
	public void add(T t) {
		collection.add(t);
	}
}
class Adapter{
	public static <T> AddableCollectionAdapter<T> collectionAdapter(Collection<T> collection) {
		return new AddableCollectionAdapter<T>(collection);
	}
}

